﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Steeringengine.util
{
    internal class DataUtil
    {
        public static Int32 HexStringToInt(String hexStr)
        {
            byte[] arr = new byte[hexStr.Length / 2];
            for (int i = 0; i < hexStr.Length / 2; i++)
            {
                arr[i] = Convert.ToByte(hexStr.Substring(i * 2, 2), 16);
            }
            Int32 f1 = BitConverter.ToInt32(arr, 0);
            return f1;
        }
        public static byte[] IntToHexByte(Int32 data)
        {
            byte[] pack = new byte[2];
            byte[] a = BitConverter.GetBytes(data);
            if (data <= 255)
            {
                pack[0] = 0X00;
                pack[1] = a[0];

            }
            else {
                pack[0] = a[1];
                pack[1] = a[0];
            }
            return pack;
        }

        public static void sendData(SerialPort serialPort, byte[] data)
        {
            if (!serialPort.IsOpen)
            {
                MessageBox.Show("串口异常,请重新连接", "错误");
                return;

            }
            try
            {
                if (data.Length > 0)
                {
                    serialPort.Write(data, 0, data.Length);//发送数据
                }
            }
            catch (Exception)
            {
                MessageBox.Show("串口异常,请重新连接", "错误");
                return;
            }


        }
        public static String IntToHexString(Int32 data)
        {
            byte[] a = BitConverter.GetBytes(data);
            return ByteToHex(a);
        }

        /// <summary>
        /// Byte 数组转十六进制字符串
        /// </summary>
        /// <param name="Bytes"></param>
        /// <returns></returns>
        public static string ByteToHex(byte[] Bytes)
        {
            string str = string.Empty;
            foreach (byte Byte in Bytes)
            {
                str += String.Format("{0:X2}", Byte);
            }
            return str.Trim();
        }

        /// <summary>
        /// 字符串转十六进制Byte数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] HexStrToToHexByte(string hexString)
        {
            try
            {
                hexString = hexString.Replace(" ", "");
                if ((hexString.Length % 2) != 0)
                    hexString = "0" + hexString;
                byte[] returnBytes = new byte[hexString.Length / 2];
                for (int i = 0; i < returnBytes.Length; i++)
                    returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                return returnBytes;
            }
            catch
            {
                return null;
            }

        }
        /// <summary>
        /// 十六进制转为负数
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static int HexStringToNegative(string strNumber)
        {
            int iNegate = 0;
            int iNumber = Convert.ToInt32(strNumber, 16);

            if (iNumber > 127)
            {
                int iComplement = iNumber - 1;
                string strNegate = string.Empty;

                char[] binChar = Convert.ToString(iComplement, 2).PadLeft(8, '0').ToArray();

                foreach (char ch in binChar)
                {
                    if (Convert.ToInt32(ch) == 48)
                    {
                        strNegate += "1";
                    }
                    else
                    {
                        strNegate += "0";
                    }
                }

                iNegate = -Convert.ToInt32(strNegate, 2);
            }

            return iNegate;
        }
        /// <summary>
        /// 整数转负数的16进制
        /// </summary>
        /// <param name="Number"></param>
        /// <returns></returns>
        public static string NegativeToHexString(int iNumber)
        {
            string strResult = string.Empty;

            if (iNumber < 0)
            {
                iNumber = -iNumber;

                string strNegate = string.Empty;

                char[] binChar = Convert.ToString(iNumber, 2).PadLeft(8, '0').ToArray();

                foreach (char ch in binChar)
                {
                    if (Convert.ToInt32(ch) == 48)
                    {
                        strNegate += "1";
                    }
                    else
                    {
                        strNegate += "0";
                    }
                }

                int iComplement = Convert.ToInt32(strNegate, 2) + 1;

                strResult = Convert.ToString(iComplement, 16).ToUpper();
            }

            return strResult;
        }
    }
 
          
}
